***********************************************************
* This dofile creates Figure F.5
***********************************************************

use "$data_prep/owwa_data_merged_long", clear
estimates clear

* Drop DWs that never went to HK/KSA
keep if status_3 < 5 | status_3 >= .

********************************************************************************
* treatment effects regarding to time of interview

* Histograms of duration until interview (direct interveiew)
histogram duration_3 if direct_3==1, fraction width(20) title("Full") xtitle("Time until interview (days since baseline)") ytitle("Fraction") ///
	xlabel(600(100)1200, valuelabels angle(0)) ylabel(, valuelabels angle(0))  name(full_distribution_ofdays_di, replace)

histogram duration_3 if ksa==1 & direct_3==1, fraction width(20) title("Saudi Arabia") xtitle("Time until interview (days since baseline)") ytitle("Fraction") ///
	xlabel(600(100)1200, valuelabels angle(0)) ylabel(, valuelabels angle(0)) name(ksa_distribution_ofdays_di, replace)

histogram duration_3 if ksa==0 & direct_3==1, fraction width(20) title("Hong Kong") xtitle("Time until interview (days since baseline)") ytitle("Fraction") ///
	xlabel(600(100)1200, valuelabels angle(0)) ylabel(, valuelabels angle(0)) name(hk_distribution_ofdays_di, replace)

graph combine full_distribution_ofdays_di  hk_distribution_ofdays_di ksa_distribution_ofdays_di, row(3) xsize(1) ysize(1.5) iscale(0.5) title("Distribution") name(d_all_together, replace)

* 
forvalues j=700(30)1200 {
forvalues i = 2(-1)0 {
	if `i'==2 local sample = "All"
	else if `i'==1 local sample = "HK"
	else if `i'==0 local sample = "KSA"
	display "`sample'"
	
	preserve
	drop if ksa==`i'
	tab ksa

	
	regout std_treat $controls talk_employer if wave==3 & direct_3==1 & duration_3<=`j' & gift<., cluster(rno) treat(gift) estname(`sample'`j')
	parmest, label level(90 95) list(parm label estimate min* max* p ) saving("$outpath_fig/`sample'_`j'", replace)
	
	restore
}
}
local fnames: dir "$outpath_fig" files "*.dta"
display `fnames'
foreach file of local fnames {
    use "$outpath_fig/`file'", clear
	display `"`file'"'
	gen days=substr("`file'", strpos("`file'", "_")+1,strpos("`file'", ".")-strpos("`file'", "_")-1 )
	destring days, replace
	gen sample=substr("`file'",1, strpos("`file'", "_")-1)
	keep if parm=="gift"     
	encode parm, generate(parm2)
	drop parm
	rename parm2 parm
	save, replace
}

clear 
local fnames: dir "$outpath_fig" files "*.dta"
foreach file of local fnames {
        append using "$outpath_fig/`file'"
     }
		
twoway (scatter estimate days if sample =="KSA", legend(off) msize(vsmall) mcolor(navy) lcolor(navy) yline(0, lcolor(gray)) ) ///
	(rcap min90 max90 days if sample =="KSA", lcolor(maroon)), xlabel(, valuelabels angle(0)) xtitle("Time until interview (days since baseline)") ///
	ytitle("Treatment effect" )  xlabel(700(100)1200, valuelabels angle(0)) ylabel(, valuelabels angle(0))  title("Saudi Arabia") name(ksa_sample, replace)
twoway (scatter estimate days if sample =="HK", legend(off) msize(vsmall) mcolor(navy) lcolor(navy) yline(0, lcolor(gray)) ) ///
	(rcap min90 max90 days if sample =="HK", lcolor(maroon)), xlabel(, valuelabels angle(0)) xtitle("Time until interview (days since baseline)") ytitle("Treatment effect") ///
	 xlabel(700(100)1200, valuelabels angle(0)) ylabel(, valuelabels angle(0))  title("Hong Kong")  name(hk_sample, replace)
twoway (scatter estimate days if sample =="All", legend(off) msize(vsmall) mcolor(navy) lcolor(navy) yline(0, lcolor(gray))) ///
	(rcap min90 max90 days if sample =="All", lcolor(maroon)), xlabel(, valuelabels angle(0)) xtitle("Time until interview (days since baseline)") ytitle("Treatment effect")  ///
		xlabel(700(100)1200, valuelabels angle(0)) ylabel(, valuelabels angle(0))  title("Full") name(full_sample, replace) 
graph combine full_sample hk_sample  ksa_sample, row(3) xsize(1) ysize(1) iscale(0.5) title("Treatment effect") name(all_together, replace)

graph combine all_together  d_all_together , cols(2) xsize(1) ysize(1) 
graph export "$outpath_fig/figure_f5.eps", replace
graph export "$outpath_fig/figure_f5.pdf", replace


clear
cd "$outpath_fig"
local fnames: dir "$outpath_fig" files "*.dta"
foreach file of local fnames {
    capture erase "`file'"
}


